package com.ccp.dev.workflow.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.form.model.SubTableRights;
import com.ccp.dev.form.service.SubTableRightsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * ClassDescribe:
 * 对象功能:子表权限 控制器类
 * @author :wangcheng
 * Date: 2019-11-21
 */
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_WORKFLOW + "/subTableRights")
@Controller
@Action(ownermodel = SysAuditModelType.PROCESS_MANAGEMENT)
public class SubTableRightsController extends SysBaseController {
    @Resource
    private SubTableRightsService subTableRightsService;

    /**
     * 添加或更新子表权限
     * @param request http请求
     * @param subTableRights 子表权限对象
     * @return 结果集封装
     */
    @RequestMapping("/add")
    @ResponseBody
    @Action(description = "添加或更新子表权限", detail = "添加或更新子表权限")
    public ResultData save(HttpServletRequest request, SubTableRights subTableRights) {
        try {
            String parentProcDefId = RequestUtil.getString(request, "parentProcDefId");
            SubTableRights tableRights;
            if(StringUtil.isEmpty(parentProcDefId)){
                tableRights = subTableRightsService.getByProcDefIdAndNodeId(subTableRights.getProcDefId(),
                        subTableRights.getNodeId(), subTableRights.getTableId());
            }else{
                tableRights = subTableRightsService.getByProcDefIdAndNodeIdAndParent(subTableRights.getProcDefId(),
                        subTableRights.getNodeId(), subTableRights.getTableId(), parentProcDefId);
            }

            if (tableRights != null) {
                subTableRights.setId(tableRights.getId());
                subTableRightsService.update(subTableRights);
                return getResultDataSuccess("update.success");
            }else{
                subTableRights.setId(UUIDUtils.getUUIDFor32());
                subTableRightsService.add(subTableRights);
                return getResultDataSuccess("save.success");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return getResultDataError("operation.failure");
        }
    }

    /**
     * 删除子表权限
     * @param request
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    @Action(description = "删除子表权限", detail = "删除子表权限")
    public ResultData delete(HttpServletRequest request) {
        try {
            String id = RequestUtil.getString(request, "id");
            subTableRightsService.delById(id);
            return getResultDataSuccess("delete.success");
        } catch (Exception e) {
            e.printStackTrace();
            return getResultDataError("operation.failure");
        }
    }

    @RequestMapping("/detail")
    @ResponseBody
    @Action(description = "查看子表权限明细", detail = "查看子表权限明细")
    public ResultData detail(HttpServletRequest request) {
        String tableId = RequestUtil.getString(request, "tableId");
        String nodeId = RequestUtil.getString(request, "nodeId");
        String procDefId = RequestUtil.getString(request, "procDefId");
        String parentProcDefId = RequestUtil.getString(request, "parentProcDefId");
        SubTableRights subTableRights;
        if (StringUtil.isEmpty(parentProcDefId)) {
            subTableRights = subTableRightsService.getByProcDefIdAndNodeId(procDefId, nodeId, tableId);
        }else{
            subTableRights = subTableRightsService.getByProcDefIdAndNodeIdAndParent(procDefId, nodeId, tableId, parentProcDefId);
        }

        StringBuilder sb = new StringBuilder("{success:true");
        if(subTableRights!=null){
            sb.append(",\"id\":\"").append(subTableRights.getId()).append("\",")
                    .append("\"permissionType\":").append(subTableRights.getPermissionType()).append(",")
                    .append("\"permissionSetting\":\"")
                    .append(subTableRights.getPermissionSetting()!=null?
                            subTableRights.getPermissionSetting().replaceAll("\n", "<br>").replaceAll("[\"]", "<032>"):"")
                    .append("\"");
        }
        sb.append("}");
        return getResultDataSuccess("operation.success", sb.toString());
    }
}
